
<extend name="Public:base" />
<block name="javascript">

    <style type="text/css">
        .list-nav{
            background-color: #f8f8f8;
            padding-right: 0;
            padding-left: 0;
        }
        .list-nav ul{
            margin-left:0;
        }
        .list-nav ul li {
            border-bottom: 1px solid #e7e7e7;
        }

        .list-nav ul li a.active {
            background-color: #eee;
        }

        .list-nav .nav-second-level li,
        .list-nav .nav-third-level li {
            border-bottom: 0!important;
        }

        .list-nav .nav-second-level li a {
            padding-left: 37px;
        }

        .list-nav .nav-third-level li a {
            padding-left:  62px;
        }
        input[type=checkbox].ace + .lbl::before, input[type=radio].ace + .lbl::before{
            vertical-align:text-top;
        }
        .nav:before, .nav:after{
            display: none;
        }
    </style>
    <script type="text/javascript">


    var idAllString = [];
    $(document).ready(function(){


     $.ajax({
         type: "POST",
         // data: {},
         url:"{:U('AuthGroup/treeList')}",
         dataType: "json",
         success: function(data){

            $("#side-menu ul").append(dataHtml(data.err_msg));

            $("#side-menu ul>li").on("click","a",function(e){
                e.stopPropagation();

                var target = e.currentTarget;

                var parent = $(target).closest("li");

                //slideToLocation("#" + $(parent).attr("id"));

                if ($(parent).parent().parent().attr("id") == "side-menu") {
                    $(target).addClass("active");
                    $(parent).siblings().find("a:first-of-type").removeClass("active");
                }

                var ulMenu = $(target).siblings("ul");

                if ($(ulMenu).is(":visible")) {
                    $(target).find("i:last-of-type").removeClass("icon-chevron-down").addClass("icon-chevron-left");
                    $(ulMenu).slideUp(300);
                }else{
                        $(parent).siblings().find("a i:last-of-type").removeClass("icon-chevron-down").addClass("icon-chevron-left");
                    $(parent).siblings().find("ul").slideUp(300);
                    $(target).find("i:last-of-type").removeClass("icon-chevron-left").addClass("icon-chevron-down");
                    $(ulMenu).slideDown(300);
                }
            });


            $("#side-menu ul>li").on("click","label input",function(e){


                var target = e.currentTarget;

                //父级label元素
                var parent = $(target).parent();

                if (target.checked) {
                    $(parent).nextAll("label").find("input").prop('checked', this.checked);
                }else{
                    $(parent).prevAll("label").find("input").prop('checked', this.checked);
                }

            });


            // $("#side-menu ul>li label input").addClass("disabled").attr({"disabled":"disabled"}).css({"cursor": "not-allowed"});

         },
         error: function(data){
            alert("请求发生错误，请刷新");
         }
        });

    function getValue(){
            var idArr = [];
            var idAllArr = [];
            //ids当前元素的id值 idParent父亲的id idParents爷爷的id modificationValue 1为不修改 0为修改
            var lookValue,modificationValue,removeValue;

            //遍历所有选中的chexked  父级的li的id的值
            var id = [];
            $("#side-menu ul>li a label input").each(function(i,element){
                if (element.checked) {
                    id.push($(element).closest("li").attr("id"));
                }

            });

            $.each(unique(id),function(i,element){
                //ids当前元素的id值 idParent父亲的id idParents爷爷的id
                var ids = "",
                    idParent = "",
                    idParents = "";


                ids = $("#" + element).attr("id");

                idParent = $("#" + element).closest("ul").parent().attr("id");

                if (!($("#" + element).closest("ul").parent().parent().hasClass("nav in"))) {
                    idParents = $("#" + element).closest("ul").closest("li").parent().parent().attr("id");
                }

                if ($("#" + element).find("label input")[2].checked) {
                    lookValue = 1;
                    modificationValue = 1;
                    removeValue = 1;
                }else if($("#" + element).find("label input")[1].checked){
                    lookValue = 1;
                    modificationValue = 1;
                    removeValue = 0;
                }else if($("#" + element).find("label input")[0].checked){
                    lookValue = 1;
                    modificationValue = 0;
                    removeValue = 0;
                }

                idArr = [];
                idArr.push(idParents);
                idArr.push(idParent);
                idArr.push(ids);
                idArr.push(lookValue);
                idArr.push(modificationValue);
                idArr.push(removeValue);

                idAllArr.push(idArr);
            })

            idAllString = [];
            $.each(idAllArr,function(i,element){
                var ele0 = "";
                if (element[0] == "") {
                    ele0 = "";
                }else{
                    ele0 = element[0].replace("abc","") + "#";
                }
                idAllString.push(ele0 + element[1].replace("abc","") + "$" + element[2].replace("abc","") + "#" + element[3] + "_" + element[4]  + "_" + element[5]);
            })
    }

    //删除数组相同的元素  返回新的数组
    function unique(arr){
        var tmp = [];
        // 遍历arr，把元素分别放入tmp数组(不存在才放)
        var tmp = new Array();
        for(var i in arr){
            //该元素在tmp内部不存在才允许追加
            if(tmp.indexOf(arr[i])==-1){
               tmp.push(arr[i]);
            }
        }
        return tmp;
    }



    function dataHtml(data){
        var content = '';
        $.each(data,function(i,element){
            content +=  '<li id="abc' +element.id+ '">' +
                                 '<a href="#"><i class="icon-folder-open"></i> ' + element.name;
             if (element.children) {
                    content += '<i class="pull-right icon-chevron-left"></i>';
                    content += '</a>';
                    content += '<ul class="nav nav-second-level collapse" aria-expanded="true">';
                    //二级的所有li html字符串
                    var contentcCildren = "";
                  $.each(element.children,function(i,element){
                    contentcCildren += '<li id="abc' +element.id+ '">' + '<a href="#">' + element.name;
                     //判断是否有三级
                         if (element.children) {
                                contentcCildren += '<i class="pull-right icon-chevron-left"></i>';
                                contentcCildren += '</a>';
                                contentcCildren += '<ul class="nav nav-third-level collapse" aria-expanded="true">';
                                //三级的所有li html字符串
                                var contentcCildren3 = '';
                              $.each(element.children,function(i,element){
                                contentcCildren3 += '<li id="abc' +element.id+ '">' +
                                             '<a href="#">' + element.name;
                                    contentcCildren3 += '<label class="pull-right" style="margin-left: 10px">' +
                                                                        '<input name="form-field-checkbox" type="checkbox" class="ace remove">' +
                                                                        '<span class="lbl"> 删除</span>' +
                                                                    '</label>' +
                                                                    '<label class="pull-right" style="margin-left: 10px">' +
                                                                        '<input name="form-field-checkbox" type="checkbox" class="ace modification">' +
                                                                        '<span class="lbl"> 修改</span>' +
                                                                    '</label>' +
                                                                    '<label class="pull-right">' +
                                                                        '<input name="form-field-checkbox" type="checkbox" class="ace look">' +
                                                                        '<span class="lbl"> 查看</span>' +
                                                                    '</label>';
                                      contentcCildren3 += "</a></li>";
                              });
                                contentcCildren += contentcCildren3 + '</ul>';//二级ul结束
                         }else{
                              contentcCildren += '<label class="pull-right" style="margin-left: 10px">' +
                                                                '<input name="form-field-checkbox" type="checkbox" class="ace remove">' +
                                                                '<span class="lbl"> 删除</span>' +
                                                            '</label>' +
                                                            '<label class="pull-right" style="margin-left: 10px">' +
                                                                '<input name="form-field-checkbox" type="checkbox" class="ace modification">' +
                                                                '<span class="lbl"> 修改</span>' +
                                                            '</label>' +
                                                            '<label class="pull-right">' +
                                                                '<input name="form-field-checkbox" type="checkbox" class="ace look">' +
                                                                '<span class="lbl"> 查看</span>' +
                                                            '</label>';
                                contentcCildren += '</a>';
                         }
                     contentcCildren + "</li>";
                  });
                  content += contentcCildren + '</ul>';  //二级ul结束
             }else{
                 content += '<label class="pull-right" style="margin-left: 10px">' +
                                                    '<input name="form-field-checkbox" type="checkbox" class="ace remove">' +
                                                    '<span class="lbl"> 删除</span>' +
                                                '</label>' +
                                                '<label class="pull-right" style="margin-left: 10px">' +
                                                    '<input name="form-field-checkbox" type="checkbox" class="ace modification">' +
                                                    '<span class="lbl"> 修改</span>' +
                                                '</label>' +
                                                '<label class="pull-right">' +
                                                    '<input name="form-field-checkbox" type="checkbox" class="ace look">' +
                                                    '<span class="lbl"> 查看</span>' +
                                                '</label>';
                   content += '</a>';
             }
                content += '</li>';
        });

        return content;
    }


    $('#addbut').click(function(){
        getValue();
        $.ajax({
            url:"{:U('AuthGroup/addadmin')}",
            type:'post',
            data:{
                u_name:$('#u_name').val(),
                password:$('#u_password').val(),
                ids:idAllString,
                name:$('#true_name').val()
            },
            success:function(data){
                if(data==1){
                    $.jbox.info("添加成功！", 'loading');
                    window.setTimeout(function () {window.location.href="{:U('AuthGroup/addadmin')}"; }, 500);
                }else if(data==2){
                    $.jbox.info("用户名已存在！");

                }
                console.log(data)
            }
        })
    })

    // $('#addbut').click(function(){
    //     getValue();
    //     console.log(idAllString);
    // })


});

    </script>
</block>

<block name="content">
    <div class="row marginT10">
        <div class="col-xs-12">
            <form class="form-horizontal" id="form" method="post" action="/zhaoshifu/admin.php?s=/Settings/save">
                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-left">用户名： </label>
                    <div class="col-sm-10">
                        <input type="text" name="u_name" id="u_name"></label>
                    </div>
                </div>


                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-left">真实名字： </label>
                    <div class="col-sm-10">
                        <input type="text" name="true_name" id="true_name"></label>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-left">密码： </label>
                    <div class="col-sm-10">
                        <input type="text" name="u_password" id="u_password"></label>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-left">列表： </label>
                    <div class="col-sm-5">
                        <div class="list-nav" id="side-menu">
                            <ul class="nav in">

                            </ul>
                        </div>
                    </div>
                </div>

                <div class="form-group">
                    <label class="col-sm-2 control-label no-padding-left" style="visibility: hidden;">提交：</label>
                    <div class="col-sm-10">
                        <input type="button" value="添加" id="addbut" class="btn btn-primary radius5 btn-sm" >
                    </div>
                </div>

            </form>
        </div>
    </div>
</block>

